package com.hwadee.ems.service.impl;

import java.util.List;
import java.time.LocalDate;

import jakarta.annotation.Resource;
import jakarta.annotation.PostConstruct;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.dto.PageQuery;
import com.hwadee.ems.domain.po.Approval;
import com.hwadee.ems.domain.po.PaperApplication;
import com.hwadee.ems.domain.vo.PaperApplicationVO;
import com.hwadee.ems.enums.ApprovalType;
import com.hwadee.ems.enums.ApprovalStatus;
import com.hwadee.ems.mapper.PaperApplicationMapper;
import com.hwadee.ems.service.IApprovalService;
import com.hwadee.ems.service.IPaperApplicationService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 系部试卷提交申请 服务实现类
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Service
public class PaperApplicationServiceImpl extends ServiceImpl<PaperApplicationMapper, PaperApplication> implements IPaperApplicationService {

    private static IPaperApplicationService instance;

    public static IPaperApplicationService getInstance() {
        return instance;
    }

    @PostConstruct
    void init() {
        instance = this;
    }

    @Resource
    private IApprovalService approvalService;

    @Override
    @Transactional
    public Result<Long> saveWithApproval(PaperApplication paperApplication) {
        Approval approval = Approval.builder()
            .type(ApprovalType.PAPER_SUBMISSION)
            .status(ApprovalStatus.PENDING)
            .build();
        approvalService.save(approval);
        paperApplication.setApprovalId(approval.getId());
        paperApplication.setApplicationDate(LocalDate.now());
        if (save(paperApplication)) {
            return Result.ok(paperApplication.getId());
        } else {
            return Result.error();
        }
    }

    @Override
    public Result<IPage<PaperApplicationVO>> page(
        PageQuery pageQuery, 
        String courseName, 
        String approverName,
        Integer approvalStatus,
        String className
    ) {
        List<PaperApplicationVO> vos = baseMapper.selectPageByConditions(
            null,
            courseName, 
            approverName, 
            approvalStatus,
            className,
            pageQuery.getSize(),
            (pageQuery.getCurrent() - 1) * pageQuery.getSize()
        );

        IPage<PaperApplicationVO> page = Page.of(pageQuery.getCurrent(), pageQuery.getSize());
        page.setTotal(count());
        page.setRecords(vos);

        return Result.ok(page);
    }

    @Override
    public Result<IPage<PaperApplicationVO>> pageByApplicantId(
        PageQuery pageQuery, 
        Long applicantId, 
        String courseName,
        String approverName, 
        Integer approvalStatus,
        String className
    ) {
        List<PaperApplicationVO> vos = baseMapper.selectPageByConditions(
            applicantId, 
            courseName, 
            approverName, 
            approvalStatus, 
            className,
            pageQuery.getSize(),
            (pageQuery.getCurrent() - 1) * pageQuery.getSize()
        );

        IPage<PaperApplicationVO> page = Page.of(pageQuery.getCurrent(), pageQuery.getSize());
        page.setTotal(count());
        page.setRecords(vos);

        return Result.ok(page);
    }

}
